home *** CD-ROM | disk | FTP | other *** search
- #!/usr/bin/perl5
- #
- # ftp-anonymous.cgi
- #
- # Copyright 1988-1996 Silicon Graphics, Inc.
- # All rights reserved.
- #
- # This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- # the contents of this file may not be disclosed to third parties, copied or
- # duplicated in any form, in whole or in part, without the prior written
- # permission of Silicon Graphics, Inc.
- #
- # RESTRICTED RIGHTS LEGEND:
- # Use, duplication or disclosure by the Government is subject to restrictions
- # as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- # and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- # successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- # rights reserved under the Copyright Laws of the United States.
- #
- # $Id: ftp-anonymous.frm,v 1.40 1997/06/24 22:20:39 shotes Exp $
-
- # flow chart on form validation:
- #
- # if (ftp user does not already exist)
- # if (home directory already exists) -> error
- # else -> create directory and ftp user
- # else
- # if (home directory exists and owned by ftp) -> check for nec. files
- # elsif (home dir. exists and not owned by ftp) -> error
- # else -> create home dir. and add nec. files
-
- BEGIN { require "/usr/WebFace/lib/CGI.pm"; import CGI; }
- require "/usr/OnRamp/lib/OnRamp.pm";
- require "/usr/OnRamp/lib/java.pm";
-
- $query = new CGI;
-
- $passwdfile = "/etc/passwd";
- $confI = "/etc/inetd.conf";
- $dummy = "/etc/passwd.tmp";
- $title = "Anonymous FTP Configuration";
-
- if ($ENV{'HTTP_USER_AGENT'} =~ /Mozilla\/2/) { $br_index = 1; }
- else { $br_index = 0; }
-
- $js =
- "br_index = $br_index;
- $js_standard
- $js_error_box
- $js_filename
- function checkForm(form) {
- if (form.eftp[br_index].checked) {
- if (!testFilename(form.home, \"home directory name\")) return (false);
- }
- return (true);
- }";
-
-
- print $query->header;
- &js_title_block($title,$js);
-
- $help = $document_root . $ENV{"SCRIPT_NAME"};
- $help =~ s/cgi$/hlp/;
- exec $help if ($query->param('help') eq "Help");
-
- # if anonymous FTP user exists, we assume directory tree
- # also exists.
-
- @getpass = getpwnam("ftp");
- if ($query->param('doit') eq 'Ok') {
- if ($getpass[0]) {
- $oldHome = $getpass[7];
- $oldEftp = 'Yes';
- }
- if ($query->param('eftp') eq 'Yes') {
- $home = $query->param('home');
- &error("Invalid home directory.") if !$home;
- &formValidation;
- &checkInetdEntry;
-
- if (!$getpass[0] || $oldHome ne $home) {
- if ($getpass[0]) { &deleteOld; }
- &add_password("ftp","*",500,25,"anon ftp",$home,"/bin/false");
- }
- if (-d $home) { &checkForFiles; }
- else { &createHome; }
- $message = "Anonymous FTP enabled.";
- } else { &disable_ftp; }
- } elsif ($getpass[0]) {
- $home = $getpass[7];
- $eftp = 'Yes';
- } else {
- $home = '/var/ftp';
- $eftp = 'No';
- }
-
- &generic;
-
- print $query->end_html;
-
- sub deleteOld {
- open(IN,"< $passwdfile");
- open(OUT,"> $dummy");
- while(<IN>) {
- @items = split(/:/);
- if ($items[0] ne "ftp") { print OUT $_; }
- }
- close(IN);
- close(OUT);
- rename($dummy,$passwdfile);
- }
-
- sub checkInetdEntry {
- $found = 0;
- $doRename = 0;
- open(IN,"< $confI");
- open(OUT,"> $dummy");
- while(<IN>) {
- @items = split(/\s+/);
- if (($items[0] eq "#" && $items[1] eq "ftp") || $items[0] eq "#ftp") {
- $_ =~ /^\s*#\s*(.*)$/;
- $line = $1;
- print OUT "$line\n";
- $found = 1;
- $doRename = 1;
- } elsif ($items[0] eq "ftp") {
- print OUT $_;
- $found = 1;
- } else { print OUT $_; }
- }
- if (!$found) {
- $doRename = 1;
- print OUT "ftp\tstream\ttcp\tnowait\troot\t/user/etc/ftpd\tftpd\n";
- }
- close(IN);
- close(OUT);
- if ($doRename) {
- rename($dummy,$confI);
- system("/etc/killall", "-HUP", "inetd");
- }
- }
-
- sub formValidation {
- if ($getpass[0]) { # ftp user exists
- if (-d $query->param('home')) {
- my($dev,$ino,$mode,$nlink,$fuid,
- $fgid,$rdev,$size,$atime,$mtime,
- $ctime,$blksize,$blocks) = stat(_);
-
- my($name,$psswd,$uuid,$ugid,$quota,
- $comment,$gcos,$dir,$shell) = getpwnam("ftp");
-
- &error("Home directory not owned by FTP")
- if $fuid != $uuid;
- } elsif (-f $query->param('home')) {
- &error("$home exists, but is not a directory.");
- }
- } else { #ftp user does not exist
- if (-d $query->param('home')) {
- &error("Home directory owned by other user.");
- } elsif (-f $query->param('home')) {
- &error("$home exists, but is not a directory.");
- }
- }
- }
-
- sub error {
- &error_block($_[0]);
- &generic;
- exit 0;
- }
-
- sub get_os_num {
- open(IN, "/sbin/uname -r |");
- $ret = <IN>; chop $ret;
- close(IN);
- $ret;
- }
-
- sub createHome {
- if (!$home) { return 0; }
- # create home directory
- system("/sbin/mkdir", "-p", $home);
- system("/sbin/chown", "ftp.ftp", $home);
-
- $os_num = &get_os_num;
-
- # create subdirectories
- mkdir("$home/bin", 0111);
- mkdir("$home/pub", 0555);
- mkdir("$home/etc", 0111);
- mkdir("$home/lib", 0555);
- mkdir("$home/dev", 0555);
-
- mkdir("$home/lib32", 0555) if ($os_num eq "6.4" || $os_num eq "6.5");
-
- system("/sbin/chown", "ftp.ftp", "$home/pub");
-
- system("/sbin/cp", "/bin/ls", "$home/bin");
- chmod 0555, "$home/bin/ls";
-
- system("/sbin/cp", "/lib/rld", "$home/lib");
- system("/sbin/cp", "/lib/libc.so.1", "$home/lib");
- chmod 0555, "$home/lib/rld", "$home/lib/libc.so.1";
-
- if ($os_num eq "6.4" || $os_num eq "6.5") {
- system("/sbin/cp", "/lib32/rld", "$home/lib32");
- system("/sbin/cp", "/lib32/libc.so.1", "$home/lib32");
- mkdir("$home/lib/iconv", 0555);
- system("/sbin/cp", "/usr/lib/iconv/iconvtab", "$home/lib/iconv");
- chmod 0555, "$home/lib/iconv/iconvtab";
- }
-
- system("/sbin/mknod", "$home/dev/zero", "c", "37", "0");
- # `mknod $home/dev/zero c 37 0`;
- chmod 0444, "$home/dev/zero";
-
- $ftp_pass = $home . "/etc/passwd";
- $ftp_grp = $home . "/etc/group";
-
- open(OUTP,"> $ftp_pass");
- open(OUTG,"> $ftp_grp");
-
- my($name,$psswd,$uuid,$ugid,$quota,
- $comment,$gcos,$dir,$shell) = getpwnam("ftp");
- print OUTP "ftp:*:${uuid}:${ugid}:anon ftp:${dir}:${shell}\n";
- print OUTG "ftp:*:${ugid}:\n";
-
- my($name,$psswd,$uuid,$ugid,$quota,
- $comment,$gcos,$dir,$shell) = getpwnam("user");
- print OUTG "user:*:${ugid}:\n";
-
- my($name,$psswd,$uuid,$ugid,$quota,
- $comment,$gcos,$dir,$shell) = getpwnam("root");
- print OUTP "root:*:${uuid}:${ugid}::/:/bin/false\n";
- print OUTG "sys:*:$ugid:\n";
-
- close(OUTP);
- close(OUTG);
- }
-
- sub checkForFiles { }
-
- sub disable_ftp {
- $value = "";
- &putpass;
- $message = "Anonymous FTP disabled.";
- }
-
- sub generic {
- &header_block("Anonymous FTP");
-
- print "<i>$message</i>";
-
- print "<form name=\"StandardForm\" method=post onSubmit=\"return runSubmit()\">";
-
- print "<center><table cellpadding=5 width=450>\n";
-
- print "<tr><th align=left>Enable anonymous FTP:</th><th align=left>\n",
- $query->radio_group(-name=>'eftp',
- -values=>['Yes','No'], -default=>$eftp),
- "</th></tr>\n";
-
- print "<tr><th align=left>Home directory for the FTP account:</th>\n",
- "<th align=left>",
- $query->textfield('home', $home),
- "</th></tr>\n";
-
- print "</table></center><br>\n";
-
- print &js_buttons('doit','Ok','onClick="markOK()"','onClick="markOther()"');
-
- print $query->endform;
-
- print $query->end_html;
- }
-
- sub putpass {
- $variable = "ftp";
- local($len) = length($variable);
-
- open(IN,"< $passwdfile") || print "unable to read $passwdfile";
- open(OUT,"> $dummy") || print "unable to write to $dummy";
- $found = 0;
-
- while (<IN>) {
- if ($variable eq substr($_,0,$len)) {
- $found = 1;
- print OUT $value;
- } else { print OUT $_; }
- }
- if ($found == 0) { print OUT $value; }
- close(OUT);
- close(IN);
- rename($dummy, $passwdfile) || print "unable to rename $dummy";
- }
-